package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Order;
import com.example.entity.User;
import com.example.entity.UserMessage;
import com.example.mapper.UserMessageMapper;
import com.example.service.UserMessageService;
import com.example.util.BeanCopyPropertiesUtils;
import com.example.vo.MemberMessageQueryVO;
import com.example.vo.PageVo;
import com.example.vo.Result;
import com.example.vo.UserMessageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author circle48
* @description 针对表【user_message】的数据库操作Service实现
* @createDate 2024-05-14 21:11:52
*/
@Service
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage>
    implements UserMessageService{

    @Resource
    private UserMessageMapper userMessageMapper;

    /**
     * 查询用户收到的信息
     * @param memberMessageQueryVO
     * @return
     */
    @Override
    public Result getMessages(MemberMessageQueryVO memberMessageQueryVO) {

        //        1.根据用户ID和状态进行查询
        LambdaQueryWrapper<UserMessage> queryWrapper = new LambdaQueryWrapper<>();
        if (memberMessageQueryVO.getUserId() != null) {
            queryWrapper.eq(UserMessage::getUserId, memberMessageQueryVO.getUserId());
        }
        if (StringUtils.hasText(memberMessageQueryVO.getStatus())) {
            queryWrapper.eq(UserMessage::getStatus, memberMessageQueryVO.getStatus());
        }
//        2.分页查询
        Page<UserMessage> page = new Page<>(memberMessageQueryVO.getPageNum(), memberMessageQueryVO.getPageSize());
        page(page, queryWrapper);

//        3.将当前页中的UserMessage对象转换为UserMessageVO对象
        List<UserMessage> messages = page.getRecords();
        List<UserMessageVO> messageVOS = BeanCopyPropertiesUtils.copyBeanList(messages, UserMessageVO.class);
        // 构建分页VO
        PageVo pageVo = new PageVo(messageVOS, page.getTotal());
        return Result.ok(pageVo);
    }

    /**
     * 修改消息状态为已读/回收站
     * @param status
     * @param messageId
     * @return
     */
    @Override
    public Boolean editStatus(int status, Integer messageId) {
        //查询消息是否存在
        UserMessage message = this.getById(messageId);
        if (message != null) {
            message.setStatus(status);
            //执行修改
            return this.updateById(message);
        }
        return false;
    }

    @Override
    public void publishIntegrationMessage(int totalPoints, User user) {
        UserMessage userMessage = new UserMessage();
        userMessage.setCreateBy(user.getUsername());
        userMessage.setCreateTime(LocalDateTime.now());
        userMessage.setDeleteFlag(false);
        userMessage.setUpdateBy(user.getUsername());
        userMessage.setUpdateTime(LocalDateTime.now());
        userMessage.setContent("您当前到账 " + totalPoints + " 积分。");
        userMessage.setUserId(user.getId().toString());
        userMessage.setUserName(user.getUsername());
        // 设置为未读状态
        userMessage.setStatus(0);
        userMessage.setTitle("积分变更通知");
        userMessage.setMessageId(UUID.randomUUID().toString());

        this.save(userMessage);
    }




    @Override
    public void sendPaymentReminder(User user, List<Order> orders) {
        UserMessage userMessage = new UserMessage();
        userMessage.setCreateBy(user.getUsername());
        userMessage.setCreateTime(LocalDateTime.now());
        userMessage.setDeleteFlag(false);
        userMessage.setUpdateBy(user.getUsername());
        userMessage.setUpdateTime(LocalDateTime.now());
        userMessage.setContent("您有订单等待付款，订单 " + orders.stream().map(Order::getOrderSn).collect(Collectors.joining(", ")) + " 已提交成功，请及时付款。");
        userMessage.setUserId(user.getId().toString());
        userMessage.setUserName(user.getUsername());
        userMessage.setStatus(0);
        userMessage.setTitle("订单等待付款提醒");
        userMessage.setMessageId(UUID.randomUUID().toString());

        userMessageMapper.insert(userMessage);
    }
}




